use crate::error::Result;
use crate::llvm_backend::LLVMBackend;
use inkwell::values::BasicValueEnum;

impl<'ctx> LLVMBackend<'ctx> {


    /// Generate is_some() method
    pub(super) fn generate_option_is_some(
        &mut self,
        option_value: BasicValueEnum<'ctx>,
    ) -> Result<BasicValueEnum<'ctx>> {
        let tag = self.extract_option_tag(option_value)?;
        let zero = self.context.i32_type().const_zero();
        let is_some =
            self.builder
                .build_int_compare(inkwell::IntPredicate::EQ, tag, zero, "is_some")?;
        Ok(is_some.into())
    }


    /// Generate is_none() method
    pub(super) fn generate_option_is_none(
        &mut self,
        option_value: BasicValueEnum<'ctx>,
    ) -> Result<BasicValueEnum<'ctx>> {
        let tag = self.extract_option_tag(option_value)?;
        let one = self.context.i32_type().const_int(1, false);
        let is_none =
            self.builder
                .build_int_compare(inkwell::IntPredicate::EQ, tag, one, "is_none")?;
        Ok(is_none.into())
    }

}
